#!/bin/bash
#Author: Milo Luo
#Goal: For create users automaticly .
#Modify Date: 30-June-2009

#########If user is not Root, exit program.#########
IsRoot=0;#Get user id and see if a root user.
if [ "$UID" -ne "$IsRoot" ]
then
	echo "Only root user can execute this script!!!!!";
	exit 1;
fi
###########Start scripting###########
	echo "Please input a user name:";

#You can use your program to assign this value,then it should work.
	read user;

	NoLoginSh=/sbin/nologin;#For nologin users. 
	UserHomeDIR=/www/$user;#User's home directory.
	Ugroup=$user;
	
#Add a group;
	groupadd $Ugroup;

#Check the if the user is exist
	cat /etc/passwd | grep '$user' > /dev/null

#"$?" variable can know if the last command is execute right.
#if it's right, return 0,else return 1.
	UserISExist=$?;

#check user if exist
	if [ "$UserISExist" = 0 ]
	then
		echo "User:$user exist!!";
	else
#Make home dir for new user.
		mkdir -p $UserHomeDIR;
		makepasswd -crypt-md5|while read passwd realpasswd ;do 
		echo "Password -> $passwd";
		echo "Realpassword -> $realpasswd";
#Change the Directory's permission.
		chmod -R 700 $UserHomeDIR;
#Add a new user.
		useradd -g $Ugroup -s /sbin/nologin -p $realpasswd -d $UserHomeDIR $user;
#Chanage Home Dir Owner to the user.
		chown -R $user.$Ugroup $UserHomeDIR
		
		done
		echo "User:$user added!";	
	fi

#######################End Scripting##############################
